# -*- coding: utf-8 -*-
# Part of Odoo. See LICENSE file for full copyright and licensing details.

from odoo import api, fields, models, _

import datetime



import logging
_logger = logging.getLogger(__name__)

class CrEmpHelp(models.Model):

    _name = "cr.emp.help"
    _description = "帮促"

    name = fields.Char(string='姓名',related='staff_id.name',store=True)
    card_id = fields.Char(string='工号',related='staff_id.card_id',store=True)
    ws_id = fields.Many2one('cr.emp.workshop', string='所属车间', related='staff_id.ws_id',store=True)
    team_id = fields.Many2one("cr.emp.team", string='班组', related='staff_id.team_id',store=True)
    post_id = fields.Many2one("cr.emp.post", string='岗位', related='staff_id.post_id',store=True)
    staff_id = fields.Many2one('cr.emp.staff',string='员工')

    time = fields.Datetime(string='记录时间',store=True)
    lose_detail_ids = fields.One2many('cr.emp.lose.item','help_id',string='预警条目')
    year = fields.Char(string='年份',compute='_compute_time',store=True)
    month = fields.Char(string='月份',compute='_compute_time',store=True)

    @api.depends('time')
    def _compute_time(self):
        for i in self:
            if i.time:
                i.year=i.time.year
                i.month=i.time.month


class CrEmpLoseItem(models.Model):

    _name = "cr.emp.lose.item"
    _description = "失信条目"

    time = fields.Date(string='日期')
    year = fields.Char(string='年份', compute='_compute_time', store=True)
    month = fields.Char(string='月份', compute='_compute_time', store=True)

    name = fields.Char(string='姓名',related='staff_id.name',store=True)
    card_id = fields.Char(string='工号',related='staff_id.card_id',store=True)
    ws_id = fields.Many2one('cr.emp.workshop', string='所属车间', related='staff_id.ws_id',store=True)
    team_id = fields.Many2one("cr.emp.team", string='班组', related='staff_id.team_id',store=True)
    post_id = fields.Many2one("cr.emp.post", string='岗位', related='staff_id.post_id',store=True)
    staff_id = fields.Many2one('cr.emp.staff',string='员工')

    help_card_id = fields.Char(string='包保干部工号')
    help_staff_id = fields.Many2one('cr.emp.staff',string='员工')
    help_name = fields.Char(string='包保干部姓名',related='help_staff_id.name',store=True)

    observe_end_time = fields.Date(string='观察期至')
    lose_fault = fields.Text(string='失信原因')
    measures = fields.Text(string='整改措施')
    help_id = fields.Many2one('cr.emp.help',string='帮促')
    
    @api.depends('help_card_id')
    def _compute_help_staff(self):
        #lqy 增加加分人
        pass

    @api.model
    def create(self, values):
        lose=super(CrEmpLoseItem, self).create(values)
        help=self.env['cr.emp.help'].search([('staff_id','=',lose.staff_id.id)])[0]
        lose.write({'help_id':help.id})
        return lose


    @api.depends('time')
    def _compute_time(self):
        for i in self:
            if i.time:
                i.year = i.time.year
                i.month = i.time.month